package de.cz3rwinski.haushaltsbuch;

import java.math.BigDecimal;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.TreeSet;

import de.cz3rwinski.haushaltsbuch.R;

import android.content.Context;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;



public class BuchungsListe  extends BaseAdapter{

	private static final int TYPE_ITEM = 0;
	private static final int TYPE_SEPARATOR = 1;
	private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
	
	private Typeface font; 

	private ArrayList<String> mData = new ArrayList<String>();
	private LayoutInflater mInflater;

	private TreeSet<Integer> mSeparatorsSet = new TreeSet<Integer>();

	public BuchungsListe(Context context) {
		//font = Typeface.createFromAsset(context.getAssets(),"Roboto-BoldItalic.ttf");
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		
	}

	public void addItem(final String item) {
		mData.add(item);
		notifyDataSetChanged();
	}

	public void addSeparatorItem(final String item) {
		mData.add(item);
		// save separator position
		mSeparatorsSet.add(mData.size() - 1);
		notifyDataSetChanged();
	}

	@Override
	public int getItemViewType(int position) {
		return mSeparatorsSet.contains(position) ? TYPE_SEPARATOR
				: TYPE_ITEM;
	}

	@Override
	public int getViewTypeCount() {
		return TYPE_MAX_COUNT;
	}

	public int getCount() {
		return mData.size();
	}

	public String getItem(int position) {
		return mData.get(position);
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		int type = getItemViewType(position);

		if (convertView == null) {
			holder = new ViewHolder();
			switch (type) {
			case TYPE_ITEM:
				convertView = mInflater.inflate(R.layout.item1_2, null);
				holder.id = (TextView) convertView.findViewById(R.id.tv_id);
				holder.datum = (TextView) convertView.findViewById(R.id.tv_datum);
				//holder.art = (TextView) convertView.findViewById(R.id.tv_art);
				holder.konto = (TextView) convertView.findViewById(R.id.tv_konto);
				holder.kategorie = (TextView) convertView.findViewById(R.id.tv_kategorie);
				holder.stichwort = (TextView) convertView.findViewById(R.id.tv_stichwort);
				holder.person = (TextView)convertView.findViewById(R.id.tv_person);
				//holder.lklasse = (TextView)	convertView.findViewById(R.id.tv_luxusklasse);
				holder.betrag = (TextView) convertView.findViewById(R.id.tv_betrag);
				holder.colorbar= (View) convertView.findViewById(R.id.v_colorbar);

				break;

			case TYPE_SEPARATOR:
				convertView = mInflater.inflate(R.layout.item2, null);
				holder.id = (TextView) convertView.findViewById(R.id.textSeparator);
				break;

			}
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		String[] splitData = mData.get(position).split("_");
		try {
			holder.id.setText(splitData[0]);
			holder.id.setTypeface(font);
			holder.datum.setText(twoSpace(splitData[1])+"."+twoSpace(splitData[2])+"."+splitData[3]);
			holder.datum.setTypeface(font);
			//holder.art.setText("Art: "+splitData[4]);
			//holder.art.setTypeface(font);
			holder.konto.setText(splitData[5]);
			holder.konto.setTypeface(font);
			holder.kategorie.setText("Kategorie: "+splitData[6]);
			holder.kategorie.setTypeface(font);
			holder.stichwort.setText(splitData[7]);
			holder.stichwort.setTypeface(font);
			holder.person.setText("Person: "+splitData[8]);
			holder.person.setTypeface(font);
			//holder.lklasse.setText("Luxusklasse: "+splitData[9]);
			//holder.lklasse.setTypeface(font);
			holder.betrag.setText(convertIntoCurrency(splitData[10]));
			holder.betrag.setTypeface(font);
			if(Double.parseDouble(splitData[10])>=0){
				holder.colorbar.setBackgroundColor(Color.parseColor("#00FF00"));
			}else{
				holder.colorbar.setBackgroundColor(Color.parseColor("#FF0000"));
			}
		} catch (Exception e) {

		}

		// holder.textView.setText(mData.get(position));
		// holder.textView.setTypeface(font);
		return convertView;
	}

	public class ViewHolder {
		public View colorbar;
		public TextView id;
		public TextView datum;
		/*public TextView tag;
		public TextView monat;
		public TextView jahr;*/
		//public TextView art;
		public TextView konto;
		public TextView kategorie;
		public TextView stichwort;
		public TextView person;
		//public TextView lklasse;
		public TextView betrag;
	}
	
	public String convertIntoCurrency(String input) {
		double bestand = Double.parseDouble(input) * 100;
		BigDecimal parsed = new BigDecimal(bestand).setScale(2,
				BigDecimal.ROUND_FLOOR).divide(new BigDecimal(100),
				BigDecimal.ROUND_FLOOR);
		String formato = NumberFormat.getCurrencyInstance().format(parsed);

		return formato;
	}
	
	public String twoSpace(String string){
		if(Integer.parseInt(string)<10){
			string="0"+string;
		}

		return string;
	}
}
